package cn.stard.basic.controller;





import cn.stard.basic.dto.Longin;
import cn.stard.basic.dto.Wecheatdto;
import cn.stard.basic.exception.BusinessException;
import cn.stard.basic.service.ILonginservice;

import cn.stard.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class loginController {
 @Autowired
 private ILonginservice longinservice;


    @PostMapping("/account")
    public AjaxResult logininfo(@RequestBody Longin longin){
        try {
            /*调用业务层方法发送短信验证码*/
            HashMap<String, Object> map = longinservice.logininfo(longin);
            return AjaxResult.me().setResultObj(map);
            /*设置一个运行时异常来给判断出错的地方回馈到前端*/
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙请重试");

        }
    }


    /*微信登录端口*/
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Wecheatdto wecheatdto){
        try {
         /*因为要返回多个参数所以controller层返回不太方便,所以这边让后台来返回*/
            return  longinservice.wechat(wecheatdto);
            /*设置一个运行时异常来给判断出错的地方回馈到前端*/
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙请重试");

        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody Wecheatdto wecheatdto){
        try {
            Map<String, Object> map = longinservice.binder(wecheatdto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
